const PassageModel = require('../model/passage')

module.exports = (router) => {
    // 发布或者下架文章
    router.post('/isAuthorPas', function (req, res) {
        let { _id, isAuthor } = req.body
        console.log(_id, isAuthor)
        PassageModel.findOneAndUpdate({ _id }, { isAuthor: !isAuthor }, function (err, pas) {
            if (!err) {
                res.send({ code: 0, msg: isAuthor ? '下架成功' : '上架成功' })
            } else {
                res.send({ code: 1, msg: '后端错误' })
            }
        })
    })
    // 文章模糊查询
    router.post('/searchPas', function (req, res) {
        let { search } = req.body
        let reg = new RegExp(req.body.search, 'i')
        PassageModel.find({
            $or: [
                { title: { $regex: reg } }
            ]
        }, function (err, list) {
            if (!err) {
                PassageModel.findOne({ _id: search }, function (innerErr, inner) {
                    if (inner) {
                        res.send({ code: 0, data: [inner] })
                    } else {
                        res.send({ code: 0, data: list, innerErr })
                    }
                })
            } else {
                res.send({ code: 1, msg: '后端出错', err })
            }
        })
    })
}